package swym.client;

import java.util.HashMap;
import java.util.Iterator;
import java.io.*;

/**
 * This settings class provides a map-based interface for application settings. 
 * Remember to call writeSettings() prior to application exit to save the settings.
 * @author Matt Schaefer
 * @version 1.0, 2007.12.17
 * This class is synchronized across all threads.
 */
public class Settings
{
	private static final Object propertySyncObject = new Object();
	private static HashMap<String, String> properties  = new HashMap<String, String>();
	private static final String CONFIG_FILE_NAME = "config.txt";
	private static final char SEPARATOR  = ':';
	
	/**
	 * Private constructor - no instantiation!
	 */
	private Settings()
	{}
	
	/**
	 * Retrieves a property value in the settings map. This is a synchronized
	 * wrapper for the <code>HashMap.get()</code> method.
	 * @param name the name of the setting property to retrieve
	 * @return the corresponding setting value
	 */
	public static String getProperty( String name )
	{
		synchronized( propertySyncObject )
		{
		    String strValue = properties.get( name );
		    
		    if ( strValue == null ) strValue = "";
		    
			return strValue;
		}
	}
	
	/**
	 * Sets a property value in the settings map. This is a synchronized
	 * wrapper for the <code>HashMap.put()</code> method.
	 * @param name the name of the setting property to set
	 * @param value the value of the property corresponding to <code>name</code>
	 */
	public static void setProperty( String name, String value )
	{
		synchronized( propertySyncObject )
		{
			properties.put( name, value );
		}
	}
	
	/**
	 * Attempts to load the settings stored in the default settings file. This is
	 * synchronized across all threads. It will fail silently, by the way.
	 */
	public static void loadSettings(File folder)
	{
		synchronized( propertySyncObject )
		{
			FileReader     fileReader = null;
			BufferedReader bufferedReader = null;
			try
			{
				fileReader     = new FileReader( folder.getAbsolutePath() + 
												 File.separator + 
												 CONFIG_FILE_NAME );
				bufferedReader = new BufferedReader( fileReader );
				
				String line = bufferedReader.readLine();
				
				while ( line != null )
				{
					int iSeparatorIndex = line.indexOf( SEPARATOR );
					
					if ( iSeparatorIndex > 0 )
					{
						String name  = line.substring( 0, iSeparatorIndex ).trim();
						String value = line.substring( iSeparatorIndex + 1 ).trim();
						properties.put( name, value );
					}
					line = bufferedReader.readLine();
				}
			}
			catch ( FileNotFoundException e )
			{}
			catch ( IOException e )
			{}
			finally
			{
				try
				{
					if ( bufferedReader != null )
						bufferedReader.close();
				}
				catch ( IOException e ) {}
			}
		}
	}
	
	/**
	 * Saves the settings/properties map to the default config file from which
	 * <code>loadSettings()</code> loads data. This, too, will fail silently.
	 */
	public static void writeSettings(File folder)
	{
		synchronized( propertySyncObject )
		{
			FileOutputStream fileOutputStream = null;
			PrintStream      filePrintStream = null;
			
			try
			{
				fileOutputStream      = new FileOutputStream( folder.getAbsolutePath() + 
														 	  File.separator + 
														      CONFIG_FILE_NAME );
				filePrintStream       = new PrintStream( fileOutputStream );
				Iterator<String> keys = properties.keySet().iterator();
				String separator      = Character.toString( SEPARATOR );
				
				while ( keys.hasNext() )
				{
					String name = keys.next();
					filePrintStream.println( name + 
											 separator + 
											 " " + 
											 properties.get( name ) ); 
				}
			}
			catch ( FileNotFoundException e )
			{}
			
			finally
			{
				if ( filePrintStream != null )
					filePrintStream.close();
			}
		}
	}
}

